package cn.imnaer.service.rpc.share.process;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import cn.imnaer.common.factory.TimeHelper;
import cn.imnaer.database.BsonObjectBuilder;
import cn.imnaer.service.persistence.model.SharePhoto;
import cn.imnaer.service.persistence.model.SharePhotoComment;
import cn.imnaer.service.persistence.model.SharePhotoItem;
import cn.imnaer.service.persistence.model.base.BaseSharePhoto;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTO;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTOCOMMENT;
import cn.imnaer.service.rpc.common.BaseContainImageAdapter;
import cn.imnaer.service.rpc.share.aop.SharePhotoComponentPushService;
import cn.imnaer.service.rpc.share.dao.SharePhotoCommentDaoImpl;
import cn.imnaer.service.rpc.share.dao.SharePhotoDaoImpl;

public class SharePhotoAdapter implements BaseContainImageAdapter<SharePhoto> {
	private   SharePhoto   sharePhoto;
	private  SharePhotoAdapter(SharePhoto sharePhoto) {
		this.sharePhoto=sharePhoto;
	}
     public  static  SharePhotoAdapter newInstance(SharePhoto sharePhoto) {
 		return  new  SharePhotoAdapter(sharePhoto);
     }
	public BaseSharePhoto getBaseSharePhoto() {
		BaseSharePhoto baseSharePhoto = new BaseSharePhoto();
		baseSharePhoto.setSharePhotoId(this.sharePhoto.getSharePhotoId());
		baseSharePhoto.setPlaceId(this.sharePhoto.getPlaceId());
		baseSharePhoto.setType(this.sharePhoto.getType());
		baseSharePhoto.setUserId(this.sharePhoto.getUserId());
		baseSharePhoto.setSharePhotoName(this.sharePhoto.getSharePhotoName());
		baseSharePhoto.setImage(this.getImage());
		return baseSharePhoto;
	}

	@Override
	public String getImage() {

		List<SharePhotoItem> items = this.sharePhoto.getItems();
		return items.get(0).getImage();
	}

	@Override
	public String getItemImage(String  itemId) {
		List<SharePhotoItem> items = this.sharePhoto.getItems();
		for (SharePhotoItem sharePhotoItem : items) {
			if (sharePhotoItem.getSharePhotoItemId().equals(itemId)) {
				String image = sharePhotoItem.getImage();
				return image;
			}
		}
		return null;
	}

	@Override
	public List<String> getItemSupportUsers(String itemId) {
		SharePhotoItem sharePhotoItem = null;
		for (SharePhotoItem item : sharePhoto.getItems()) {
			if (item.getSharePhotoItemId().equals(itemId)) {
				sharePhotoItem = item;
				break;
			}
		}
		List<String> supportUsers = sharePhotoItem.getSupportUsers();
		return supportUsers;
	}

	@Override
	public Collection<String> getItemInvolvedUsers(String itemId) {
		List<String> userIds = new ArrayList<>();
		BSONObject query = new BasicBSONObject();
		query.put(SHAREPHOTOCOMMENT.sharePhotoItemId, itemId);
		BSONObject selector = new BasicBSONObject();
		selector.put("userId", "userId");
		selector.put("replyUserId", "replyUserId");
		List<SharePhotoComment> record = SharePhotoCommentDaoImpl.getInstance().findAll(query, selector, null);
		for (SharePhotoComment sharePhotoComment : record) {
			String userId = sharePhotoComment.getUserId();
			String replyUserId = sharePhotoComment.getReplyUserId();
			if (replyUserId != null && !replyUserId.isEmpty()) {
				userIds.add(replyUserId);
			}
			userIds.add(userId);
		}
		userIds.addAll(this.getItemSupportUsers(itemId));
		HashSet<String> set = new HashSet<>(userIds);
		return  set;
	}

	@Override
	public String[] getAllInvolvedUsers() {
		return null;
	}

	@Override
	public Object getBaseModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAuthor() {
		// TODO Auto-generated method stub
		return this.sharePhoto.getUserId();
	}
	@Override
	public SharePhoto getModel() {
		return this.sharePhoto;
	}

}
